package day52.pet.service.impl;

import day52.basic.util.AjaxResult;
import day52.pet.domain.Pet;
import day52.pet.domain.PetDetail;
import day52.pet.mapper.PetDetailMapper;
import day52.pet.mapper.PetMapper;
import day52.pet.service.IPetService;
import day52.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yang
 * @since 2022-12-28
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;


    @Override
    @Transactional
    public void add(Pet pet) {
        petMapper.add(pet);
        PetDetail petDetail = pet.getDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.add(petDetail);
    }

    @Override
    public void onsale(List<Long> ids) {
        ids.forEach(id->{
            Pet pet = petMapper.findOne(id);
            pet.setState(1);
            pet.setOnsaletime(new Date());
            petMapper.update(pet);
        });
    }

    @Override
    public void offsale(List<Long> ids) {
        ids.forEach(id->{
            Pet pet = petMapper.findOne(id);
            pet.setState(0);
            pet.setOffsaletime(new Date());
            petMapper.update(pet);
        });
    }
}
